﻿/*
 * Copyright 2008 Lucifer Lu
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

using System;
using System.Windows;
using SilverChart.Core.Log;

namespace SilverChart.Components {
    public abstract class AlignmentComponent : SimpleChartComponent {
        private static Logger logger = Logger.GetLogger<AlignmentComponent>();

        protected String componentName;

        public AlignmentComponent(UIElement element, String name) {
            componentName = name;
            if (element != null) {
                Content = element;
            }
        }

        public override string WidgetName {
            get {
                return String.Format("{0} Alignment[{1}]", Dock, componentName);
            }
        }

        protected override Size MeasureOverride(Size availableSize) {
            if (Content != null) {
                logger.Debug("MeasureOverride():" + availableSize.ToString());
                Content.Measure(availableSize);
                logger.Debug("Desired Size:" + Content.DesiredSize);
                logger.Debug("Measure finished!");
                Double width = Content.DesiredSize.Width;
                if (!Double.IsInfinity(availableSize.Width)) {
                    width = availableSize.Width;
                }
                return new Size(width, Content.DesiredSize.Height);
            }

            return new Size(0, 0);
        }

        protected override Size ArrangeOverride(Size finalSize) {
            if (Content != null) {
                logger.Debug("ArrangeOverride() started");
                logger.Debug("finalSize:" + finalSize.ToString());
                Content.Arrange(new Rect(0, 0, finalSize.Width, finalSize.Height));
                return finalSize;
            }
            return new Size(0, 0);
        }
    }
}
